
<?php 
header("Content-type: text/html; charset=utf-8");
if($con =  mysqli_connect('localhost','root','','dlxfandb')){
		//echo "连接成功";
	}else{
		echo "连接失败";
	}
mysqli_query($con,"set names 'utf8'");
	//获取更新的识别码，1则为手动ajax，0则为定时ajax,区别开执行不同的ajax方法
	$checkcode=1;
if($checkcode){
//处理手动提交交换数据的ajax请求
    $goodID=$_GET['goodID'];
	echo $goodID;
	//从数据库取出该物品的最新价格赋值给newPrice
	//查询数据库rob_info取出初始化数据
	$query1 = 'select * from rob_info where goodID = "'.$goodID.'"';
	$result=mysqli_query($con,$query1);
    if($result){//覆盖思想
    	echo "<br>从rob_info中取出newPrice成功";
		$row  = mysqli_fetch_row($result);
		$newPrice=$row[4];
		//获取该次竞价的各种信息
		$headImgUrl=$row[6];
		$username=$row[5];
		$postTime=$row[7];
    }
	else{
		echo "取出newPrice失败";
	}
	//查询数据库rob_info取出初始化数据

	//首先写入部分初始化数据
	if($row[0]) {
		$newPrice = $row[0];
	}
	//用户竞价提升的价格
	if($_GET['inputPrice']) {
		$inputPrice = $_GET['inputPrice'];
	}
	//获取该次竞价的各种信息
	if($_GET['username']) {
		$username = $_GET['username'];
	}
	if($_GET['headImgUrl']) {
		$headImgUrl = $_GET['headImgUrl'];
	}
	if($_GET['postTime']) {
		$postTime = $_GET['postTime'];
	}
	echo $postTime;
    //运算
	$newPrice=$newPrice+$inputPrice;
	//根据goodID更新该商品在rob_info表中的最新价格
	$query2 ='update rob_info set price ='.$newPrice.' where goodID='.$goodID.'';
	$result2=mysqli_query($con,$query2);
	if($result2){
    	echo "<br>更新rob_info中newPrice成功";
    }
	else{
		echo mysqli_error();
		echo "更新rob_info中newPrice失败";
	}
	//将该次竞价记录在paid_list表中
	$query3 = 'insert into paid_list (goodID,newPrice,wechat_name,wechat_headImgUrl,postTime) values('.$goodID.','.$newPrice.',"'.$username.'","'.$headImgUrl.'","'.$postTime.'")';
	$result3=mysqli_query($con,$query3);
		if($result3){
		echo "<br>一条竞拍record插入数据库成功";
	}
	else{
		echo mysqli_error();
	    echo "<br>插入失败";
	}
	              }
else{
 //每3s自动执行一次的ajax请求
   $goodID=$_GET['goodID'];
   //根据提交的goodID查询该竞价商品的最高价格的三条喊价记录
   $query4 = 'select * from (select * from paid_list where goodID="'.$goodID.'" order by newPrice desc )as a limit 3';
	$result4=mysqli_query($con,$query4);
   if($result4){
		//echo "<br>取出某个商品的头三条竞拍信息成功";
	    $i=1;
		while($row = mysqli_fetch_array($result4)){
		     $record_Price[$i] = $row[2];
			 $record_Wechat_name[$i]=$row[3];
			 $record_Wechat_headImgUrl[$i]=$row[4];
			 $record_postTime[$i]=$row[5];
			 $i++;
			 if($i>3){
			 	break;
			 }
		}
	}
	else{
		echo mysqli_error();
	    echo "<br>取出某个商品的头三条竞拍信息失败";
	}
	//将数据打包成放入二维数组
   $backValue = array("first"=>array("record_Price"=>$record_Price[1],
                                     "record_Wechat_name"=>$record_Wechat_name[1],
									 "record_Wechat_headImgUrl"=>$record_Wechat_headImgUrl[1],
									 "record_postTime"=>$record_postTime[1]),
					   "second"=>array("record_Price"=>$record_Price[2],
                                     "record_Wechat_name"=>$record_Wechat_name[2],
									 "record_Wechat_headImgUrl"=>$record_Wechat_headImgUrl[2],
									 "record_postTime"=>$record_postTime[2]),
					    "third"=>array("record_Price"=>$record_Price[3],
                                     "record_Wechat_name"=>$record_Wechat_name[3],
									 "record_Wechat_headImgUrl"=>$record_Wechat_headImgUrl[3],
									 "record_postTime"=>$record_postTime[3]),
					    );
	//将数组格式化为JSON格式
	echo json_encode($backValue);
 
}
	
	
?>